﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Xml.Serialization;
using intelliEssay.ApplicationExceptionReportingRecordingRedistributing;

namespace intelliEssayBugCheckSystem
{
    class AutomaticMechanism
    {
        public static void Auto()
        {
            ThreadPool.QueueUserWorkItem(o =>
                {
                    Thread.Sleep(10000);
                    while (true)
                    {
                        ApplicationStates.LockIO();
                        MergeFiles();
                        //    DetectKingSoft();
                        ApplicationStates.ReleaseIO();
                        Thread.Sleep(30000);
                    }
                });
        }

        public static void DetectKingSoft()
        {
            try
            {
                System.Diagnostics.Process pro = System.Diagnostics.Process.GetProcessesByName("klive").First<System.Diagnostics.Process>();
                MainWindow.KingSoftServiceImageSourceChange(true);
            }
            catch
            {
                MainWindow.KingSoftServiceImageSourceChange(false);
            }
        }
        public static void MergeFiles()
        {
            try
            {
                IEnumerable<string> xmlFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.xml");
                xmlFiles = from file in xmlFiles where file.EndsWith(ApplicationStates.CurrentUserName + ".xml") && char.IsDigit(file[file.Length - 9]) select file;
                string latestFile = xmlFiles.Max<string>();
                if (latestFile == null) return;
                try
                {
                    File.Delete("BugCollection" + ApplicationStates.CurrentUserName + ".xml");
                }
                catch
                { }
                foreach (string file in xmlFiles)
                {
                    if (file != latestFile)
                    {
                        File.Delete(file);
                    }
                }
                File.Move(latestFile, "BugCollection" + ApplicationStates.CurrentUserName + ".xml");
            }
            catch 
            { }
        }

        internal static void MergeFilesEx()
        {
            MergeFiles();
            IEnumerable<string> xmlFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.xml").Where<string>(o =>
                {
                    return o.Contains("AdditionalBugFrom") && o.EndsWith("To" + ApplicationStates.CurrentUserID + ".xml");
                });
            foreach (string filePath in xmlFiles)
            {
                using (Stream stream = File.Open(filePath, FileMode.Open))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<intelliEssay.ApplicationExceptionReportingRecordingRedistributing.intelliEssayException>));
                    List<intelliEssay.ApplicationExceptionReportingRecordingRedistributing.intelliEssayException> temp = (List<intelliEssay.ApplicationExceptionReportingRecordingRedistributing.intelliEssayException>)xmlSerializer.Deserialize(stream);
                    BugCollection.bugCollection.AddRange(temp);
                }
                using (Stream stream = File.Create("BugCollection" + ApplicationStates.CurrentUserName + ".xml"))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<intelliEssay.ApplicationExceptionReportingRecordingRedistributing.intelliEssayException>));
                    xmlSerializer.Serialize(stream, BugCollection.bugCollection);
                }
                File.Delete(filePath);
            }
        }
    }
}
